<?php

//es全文索引在laravel中的使用流程
//es基本概念
/*
 1.索引index    相当于database
 2.类型type           table
 3.文档document       row
 4.字段field          column
 5.模板template       返回的数据


  http://localhost:9200/index/type/id   id是row的id
*/
//搜索模块的基本步骤
/*
 1.安装es和ik插件
 2.安装es的laravel scout包
 3.创建ylaravel的索引和模板
 4.将数据库的数据导入到索引中
 5.搜索页面和逻辑展示
*/

1.安装es和ik插件
/*
 步骤：下载medcl/elasticsearch -rtf
 linux下
 1.解压，删除多余插件
   bin/elasticsearch-plugin list > /tem/plugin.log
   vim /tmp/plugin.log 把其中的ik插件删除
   cat /tmp/plugin.log|xargs -I {} bin/elasticsearch-plugin remove {}
2.启动
  bin/elasticsearch -d
windows下
 1. cd bin/
    elasticsearch.bat
*/

2.安装es的laravel scout包
/*
  1.composer require laravel/scout

    Laravel\Scout\ScoutServiceProvider::class,
    php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"
  2.composer require tamayo/laravel-scout-elastic
  // config/app.php
  ScoutEngines\Elasticsearch\ElasticsearchProvider::class,

  // config/scout.php
  // Set your driver to elasticsearch
    'driver' => env('SCOUT_DRIVER', 'elasticsearch'),

...
    'elasticsearch' => [
        'index' => env('ELASTICSEARCH_INDEX', 'laravel'),
        'hosts' => [
            env('ELASTICSEARCH_HOST', 'http://localhost'),
        ],
    ],
...
*/

3.使用laravel的command实现搜索索引和模板的建立
/*
  1.composer require guzzlehttp/guzzle 利用curl发送http请求
  2.利用命令行工具php artisan make:commmand ESInit
  <?php
  namespace App\Console\Commands;
  use GuzzleHttp\Client;
  use Illuminate\Console\Command;
  class ESInit extends Command
  {

      protected $signature = 'es:init';


      protected $description = 'init es';


      public function __construct()
      {
          parent::__construct();
      }


      public function handle()
      {
          $client = new Client();
          // 创建模版http://localhost:9200/index/type/id   id是row的id
          $url = config('scout.elasticsearch.hosts')[0] . '/_template/tmp';
          $client->put($url, [
              'json' => [
                  'template' => config('scout.elasticsearch.index'),
                  'settings' => [
                      'number_of_shards' => 1
                  ],
                  'mappings' => [
                      '_default_' => [
                          '_all' => [
                              'enabled' => true
                          ],
                          'dynamic_templates' => [
                              [
                                  'strings' => [
                                      'match_mapping_type' => 'string',
                                      'mapping' => [
                                          'type' => 'text',
                                          'analyzer' => 'ik_smart',
                                          'ignore_above' => 256,
                                          'fields' => [
                                              'keyword' => [
                                                  'type' => 'keyword'
                                              ]
                                          ]
                                      ]
                                  ]
                              ]
                          ]
                      ]
                  ]
              ]
          ]);
          $this->info("=====================创建模板成功======================");
          $url = config('scout.elasticsearch.hosts')[0] . '/' . config('scout.elasticsearch.index');
          $client->put($url, [
              'json' => [
                  'settings' => [
                      'refresh_interval' => '5s',
                      'number_of_shards' => 1,
                      'number_of_replicas' => 0,
                  ],
                  'mappings' => [
                      '_default_' => [
                          '_all' => [
                              'enabled' => false
                          ]
                      ]
                  ]
              ]
          ]);
         $this->info("=====================创建索引成功======================");
      }
  }

   3.App\Console\Kernel中挂载注册 ESInit::class
   4.php artisan es:init创建一个模板和一个索引
*/

4.将数据库的数据导入到索引中
/*
 1. 配置model 如果你想要模型变得可搜索，需要添加 Laravel\Scout\Searchable trait 到模型类，该 trait 会注册模型观察者来保持搜索驱动与模型记录数据的一致性：
 <?php

namespace App;

use Laravel\Scout\Searchable;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use Searchable;

     //定义索引里的type
    public function searchableAs()
   {
       return 'posts';
   }
    //定义type里的那些字段可以搜索
   public function toSearchableArray()
    {
       return [
         'title'=>$this->title,
         'content'=>$this->content
     ];
    }
   2.索引
   批量导入

     如果你想要安装 Scout 到已存在的项目，你可能已经有了想要导入搜索驱动的数据库记录，Scout 提供了 Artisan 命令 import 用于导入所有已存在的数据到搜索索引：

     php artisan scout:import "App\Post"
}
*/

5.搜索页面和逻辑展示
 /*
    1.路由
    Route::get('/posts/search','PostController@search');
    2.控制器中
    public function search(Request $request){
          //验证
          $this->validate($request,[
          'query'=>'required',
        ]);
          //逻辑
          $posts = Post::search($request->input('query'))->paginate(10);
          //渲染
          return view('posts.search',compant('posts'));
    }


 */
